import random
import pygame
import os
import sys
class ball :
    __slots__ = ('_name','x','y','r','mx','my','color','alive')
    def __init__(self,x,y,r,mx,my,color) :
        self.x = x
        self.y = y
        self.r = r
        self.mx = mx
        self.my = my
        self.color = color
        self.alive = True
    def move(self,window) :
        self.x += self.mx
        self.y += self.my
        if self.x - self.r <= 0 or self.x + self.r >= window.get_width() :
            self.mx = -self.mx
        if self.y - self.r <= 0 or self.y + self.r >= window.get_height() :
            self.my = -self.my
    def eat(self,other) :
        if self.alive and other.alive and self.r >= other.r:
            dx,dy = abs(self.x-other.x),abs(self.y-other.y)
            distance = (dx**2 + dy**2)**0.5
            if distance < self.r + other.r :
                other.alive = False
                self.r += other.r
    def inwindow(self,window) :
        pygame.draw.circle(window,self.color,(self.x,self.y),self.r,0)
    def bug(self,window) :
        x,y = window.get_size()
        if self.x - self.r < 0 :
            self.x = self.r
        elif self.x + self.r > x :
            self.x = x - self.r
        if self.y - self.r < 0 :
            self.y = self.r
        elif self.y + self.r > y :
            self.y = y - self.r
    def boom(self,ranr,balllist) :
        if self.r > 180 :
            self.alive = False
            for _ in range(10) :
                xx = random.randint(self.x-self.r,self.x+self.r)
                yy = random.randint(self.y-self.r,self.y+self.r)
                directionx = random.randint(-2,2)
                directiony = random.randint(-2,2)
                ball2 = ball(xx,yy,ranr,directionx,directiony,ball.randomcolor())
                balllist.append(ball2)
    @staticmethod
    def randomcolor() :
        r = random.randint(0,255)
        g = random.randint(0,255)
        b = random.randint(0,255)
        return (r,g,b)
def main() :
    balllist = []
    pygame.init()
    window = pygame.display.set_mode((960,540))
    pygame.display.set_caption('ball game')
    window.fill((255,255,255))
    application_path = sys._MEIPASS
    bg = pygame.image.load(os.path.join(application_path, 'chu.png'))
    bgx,bgy = bg.get_size()
    scalepng = pygame.transform.scale(bg,(bgx/2,bgy/2))
    window_x,window_y = window.get_size()
    scalex,scaley = scalepng.get_size()
    window.blit(scalepng,((window_x-scalex)/2,(window_y-scaley)/2))
    pygame.display.flip()
    running = True
    while running :
        for event in pygame.event.get() :
            if event.type == pygame.QUIT :
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN :
                x,y = event.pos
                rancolor = ball.randomcolor()
                ranr = random.randint(10,30)
                ball1 = ball(x,y,ranr,random.randint(-2,2),random.randint(-2,2),rancolor)
                balllist.append(ball1)
        window.fill((255,255,255))
        window.blit(scalepng,((window_x-scalex)/2,(window_y-scaley)/2))
        for i in balllist :
            if i.alive :
                i.move(window)
                i.inwindow(window)
                i.bug(window)
                i.boom(ranr,balllist)
                for others1 in balllist:
                    if i != others1 and others1.alive :
                        i.eat(others1)
        pygame.display.flip()
        pygame.time.delay(15)
if __name__ == '__main__' :
    main()